<?php

namespace App\Http\Controllers\Web;

use App\Models\Users;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    //页面
    public function index(){
        return view('web.login.login');
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     * @name 登录操作
     */
    public function login(Request $request){
        //验证
        $this->validate(request(), [
            'name'=>'required|min:3',
            'password' => 'required|min:6|max:30',
            'is_remember' => '',
        ]);
        //逻辑
        //常看是否存在此用户
        $info=Users::where('name',request('name'))->first();
        if(empty($info)){
            return \Redirect::back()->withErrors("不存在此用户！");
        }
        $user['email']=$info->email;
        $user['password']=request('password');
        $remember = boolval(request('is_remember'));
        if(true==\Auth::guard('web')->attempt($user,$remember)){
            return redirect('/');
        }
        return \Redirect::back()->withErrors("用户名密码不匹配！");
    }

    /**
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     * @name 登出操作
     */
    public function logout(){
        \Auth::logout();
        return redirect('/login');
    }
}
